package com.steel.yfmall.cloud.assistant.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.steel.yfmall.cloud.assistant.BackstageConstants;
import com.steel.yfmall.cloud.assistant.api.stock.ZgSendCompanyInfoAPI;
import com.steel.yfmall.cloud.assistant.api.stock.ZgSendSmsAPI;
import com.steel.yfmall.cloud.assistant.bean.AssistantConstants;
import com.steel.yfmall.cloud.assistant.bean.param.APIParamUserCompanyBean;
import com.steel.yfmall.cloud.assistant.bean.param.zg.APISmsBean;
import com.steel.yfmall.cloud.assistant.common.CommonPublicInfoService;
import com.steel.yfmall.cloud.assistant.dto.para.user.ParaSendSmsInfoBean;
import com.steel.yfmall.cloud.assistant.dto.zg.ZgCallWsdlAPIBaseDTO;
import com.steel.yfmall.cloud.assistant.po.user.SteelCompanyCustomerCategory;
import com.steel.yfmall.cloud.assistant.po.user.SteelCompanyCustomerCategoryExample;
import com.steel.yfmall.cloud.assistant.user.SmsInfoBeanService;
import com.steel.yfmall.cloud.assistant.utils.HttpSendUtil;
import com.steel.yfmall.cloud.assistant.utils.kd.CosmicLoginUtil;
import com.steel.yfmall.cloud.assistant.utils.kd.EasLoginUtil;
import com.steel.yfmall.cloud.assistant.utils.zg.ZgCallServiceUtils;
import com.steel.yfmall.cloud.assistant.utils.zg.ZgMethodBeanUtil;
import com.steel.yfmall.cloud.common.bean.param.APIParamMallConfigBean;
import com.steel.yfmall.cloud.common.bean.result.APIResultMallConfigBean;
import com.steel.yfmall.cloud.components.utils.base.APIMsgBean;
import com.steel.yfmall.cloud.components.utils.base.ResponseCode;
import com.steel.yfmall.cloud.components.utils.utils.ResultCodeConstants;
import com.steel.yfmall.cloud.components.utils.utils.UtilDate;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: ZgSendCompanyInfoAPIImpl
 * @author: liuzhijun
 * @date: 2020-12-23 20:32
 **/
@Service
@Slf4j
public class ZgSenSmsAPIImpl implements ZgSendSmsAPI {

    @Value( value = "${yf_sms_url}")
    private String smsUrl;

    @Override
    public APIMsgBean<String> sendSms(APISmsBean apiSmsBean) {
        APIMsgBean<String> apiMsgBean = new APIMsgBean<>();
        apiMsgBean.getSoaStateBean().setCode(0);
        apiMsgBean.getSoaStateBean().setMessage("参数异常");
        if(apiSmsBean == null || StringUtils.isBlank(apiSmsBean.getPhone()) || StringUtils.isBlank(apiSmsBean.getSendMsg())){
            return apiMsgBean;
        }

        String apptoken = CosmicLoginUtil.GetAPPToken();
        String accessToken = CosmicLoginUtil.getAccessToken(apptoken);
        String url = smsUrl + "?access_token=" + accessToken;

        Map<String, Object> params = new HashMap<>();
        params.put("mobile",apiSmsBean.getPhone());
        params.put("count",apiSmsBean.getSendMsg());
        String dat = JSONObject.toJSONString(params);
        log.info("发送短信验证码参数："+JSON.toJSONString(dat));
        String result = HttpSendUtil.sendPost(url, dat);
        log.info("发送短信验证码返回参数："+JSON.toJSONString(result));
        // 发送短信验证码返回参数："{\"data\":true,\"errorCode\":\"success\",\"success\":true}\n"
        JSONObject re = JSONObject.parseObject(result);
        Object data = re.get("data");
        //{"data":true,"errorCode":"success","success":true}
        Object success = re.get("success");
        if (data == null || success == null) {
            apiMsgBean.getSoaStateBean().setMessage("发送失败");
            return apiMsgBean;
        }
        if (data.toString().equals("true") && success.toString().equals("true")) {
            apiMsgBean.getSoaStateBean().setCode(1);
            apiMsgBean.getSoaStateBean().setMessage("发送成功");
            return apiMsgBean;
        }
        return apiMsgBean;
    }
}
